<?php 



	$appId = "wxf505c02f679fdde5";
  	$appSecret = "4bbeef0cbb7441be617eac01cf37b28f ";   
    $mysql = new SaeMysql(); 
    $sql = "SELECT * FROM tb_access_token where type='access_token' LIMIT 1"; //mysql查询语句
    $data = $mysql->getData( $sql ); //执行查询语句并返回结果到变量$data
      //$data = json_decode(file_get_contents("access_token.json"));
    $oldtoken = $data[0]['access_token'];
    $time = $data[0]['expire_time'];
    if ( $time < time()) {
      // 如果是企业号用以下URL获取access_token
      // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=($appId)&secret=($appSecret)";
      $res = json_decode($this->httpGet($url));
      $access_token = $res->access_token;
      if ($access_token) {
        $time = time() + 7000;
        $sql = sprintf("update `tb_access_token`  set access_token='%s', expire_time=%d where type='access_token'",$access_token,$time);
      	$mysql->runSql($sql);
        if ($mysql->errno() != 0) //如果有错误进行提示.
        {
            echo "Error1:".$mysql->errmsg();      
        }
        else
        {
        	echo "连接成功!";
        }
        $mysql->closeDb(); //关闭连接。
      }
    } else {
      	$access_token = $oldtoken;
    }
      echo "acctoken获取成功<br/>";

?> 